<script setup>
import {ref, toRefs} from "vue";
import axios from "axios";
import {useUserStore} from "@/store/user.js";
import {removeUserFromKnowledgeBase} from "@/api/request.js";
import {showToast} from "vant";

const props = defineProps({
  user: {
    type: Object,
    default: () => {
      return {}
    }
  },
  knowledgeBaseId: {
    type: Number,
    default: 0
  }
})

const {user, knowledgeBaseId} = toRefs(props)

const store = useUserStore()

const exit = ref(true)
const DeleteUser = () => {
  removeUserFromKnowledgeBase({
    knowledgeBaseId: knowledgeBaseId.value,
    userId: store.userId
  }).then(res => {
    if (res.code === 200) {
      showToast({
        message: '删除成功',
        position: 'center',
        duration: 1000
      })
      exit.value = false
    }
  })
}
</script>

<template>
  <div class="user-item" v-if="exit">
    <div class="user-item-left">
      <van-image :src="`${axios.defaults.baseURL}/${user.avatar}`" height="2rem" round width="2rem"/>
      <div class="username">{{ user.username }}</div>
    </div>
    <div class="user-item-right">
      <van-button v-if="user.id === store.userId" size="small" type="danger" @click="DeleteUser">删除</van-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.user-item {
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0.5rem;
  box-shadow: 0 0 0.5rem rgba(0, 0, 0, 0.1);
  box-sizing: border-box;

  .user-item-left {
    width: 80%;
    display: flex;
    flex-direction: row;
    align-items: center;

    .username {
      margin-left: 0.5rem;
      font-size: 1rem;
    }
  }

  .user-item-right {
    width: 20%;
    display: flex;
    justify-content: flex-end;
  }
}
</style>